import { querySignListForZubie ,changeAthelateDatas, addSignInfo, modifySignInfo, clear, deleteSignInfo } from '../services/baoming';

export default {
  namespace: 'baoming',

  state: {
    data: {
      list: [
      ],
      pagination: { current: 1, pageSize: 20 },
    },
    zubieSignList: {},
  },

  effects: {
    *changeAthlateDatas({ payload, callback }, { call }){
      const response = yield call(changeAthelateDatas,payload)
      if(response && callback){
        callback()
      }
    },
    *addSignInfo({ payload, callback }, { select, call, put }) {
      const response = yield call(addSignInfo, payload);
      if(response){
        yield put({
          type: 'querySignListForZubie',
          payload: {
            zubieCode: payload.zubieCode
          },
        });
        if(callback) callback();
      }
    },
    *modifySignInfo({ payload,callback }, { select, call, put }) {
      const response = yield call(modifySignInfo, payload);
      if(response){
        yield put({
          type: 'querySignListForZubie',
          payload: {zubieCode: payload.zubieCode},
        });
        if(callback) callback();
      }
    },
    *clear({ payload,callback }, { select, call, put }) {
      const response = yield call(clear, payload);
      if(response){
        yield put({
          type: 'changeState',
          payload: {zubieSignList: []},
        });
        if(callback) callback();
      }
    },
    *delete({ payload,callback }, { select, call, put }) {
      const response = yield call(deleteSignInfo, payload.signInfoId);
      if(response){
        yield put({
          type: 'querySignListForZubie',
          payload: {zubieCode: payload.zubieCode},
        });
        if(callback) callback();
      }
    },
    *querySignListForZubie({ payload }, { select, call, put }) {
      const response = yield call(querySignListForZubie, payload);
      yield put({
        type: 'changeState',
        payload: {zubieSignList: response},
      });
    },
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    changeState(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
    clean(state, payload) {
      return {
        ...state,
        zubieSignList: [],
      };
    },
  },
};
